﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Framework.Data.test;
using MySql.Data.MySqlClient;

namespace Framework.Data
{
	public class DataService
	{
		string SelectSql = "SELECT {Columns} FROM {TabeleNames}";
		string SelectSqlJoin = "SELECT {Columns} FROM {TabeleNames}";
		string InserSql = "INSERT INTO {TableNames} ({Columns}) VALUES({Worths}) ";
		string Update = "UPDATE ";

	


		/// <summary>
		/// 
		/// </summary>
		/// <param name="sql"></param>
		/// <param name="conn"></param>
		/// <returns></returns>
		public int Insert(string sql, IDbConnection conn)
		{
			//using (IDbConnection conn = DapperDataService.MySqlConnection())
			//{
			//	string sql = "select ";/* * from "+TableName*/
			//						   //foreach (var item in parameter.Columns)
			//						   //{
			//						   //	if (string.IsNullOrEmpty(item))
			//						   //	{
			//						   //		//sql+=" * FROM "+TableName;
			//						   //	}
			//						   //}

			//	foreach (var item in parameter.ExistParameter)
			//	{
			//		if (string.IsNullOrEmpty(item.Key) || string.IsNullOrEmpty(item.Value))
			//		{
			//			sql += "where " + item.Key + "=" + item.Value;
			//		}
			//		else
			//		{
			//			sql += "";
			//		}
			//	}
			//	conn.Execute(sql, t);
			//}
			return 1;
		}
		
		public string EnsembleSql(CommonParameter parameter)
		{
			//string sql = "SELECT";
			//if (parameter.TableNames.Count==0)
			//{
			//	throw new Exception("请输入表名");
			//}
			//if (parameter.BackParameter.Count==0)
			//{
			//	sql += "* FROM ";
			//}
			//else
			//{
			//	foreach (var item in parameter.BackParameter)
			//	{
			//		sql += item+", ";
			//	}
			//	sql += "FROM ";
			//}
			////foreach (var item in collection)
			////{

			////}

			//foreach (var item in parameter.TableNames)
			//{
			//	sql += item;
			//}

			//return sql;


			if (!string.IsNullOrEmpty(parameter.Columns))
			{
				SelectSql = SelectSql.Replace("{Columns}", parameter.Columns);
			}
			else
			{
				SelectSql = SelectSql.Replace("{Columns}", "*");
			}
			if (parameter.TabeleNames.Count == 0)
			{
				throw new Exception("异常，表名为空");
			}
			else if (parameter.TabeleNames.Count == 1)
			{
				SelectSql.Replace("{TabeleNames}", parameter.TabeleNames[0]);
			}

			
			var sb = new StringBuilder();
			sb.Append(SelectSql);
			sb.AppendFormat(SelectSql);

			return string.Empty;
		}
	}
}
